package com.situ.crm.rbac.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm.bean.pojo.Role;
import com.situ.crm.common.utils.JSONResult;
import com.situ.crm.rbac.service.IRoleService;
import io.swagger.annotations.*;
import io.swagger.util.Json;
import jdk.nashorn.internal.scripts.JS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")//tags:你可以当做这个组的名字
@RestController
@RequestMapping("/rbac/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "分页查询", notes = "根据输入的第几页current，每页有多少条limit，返回分页信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页有多少条", required = false, defaultValue = "5")
    })
    @GetMapping("/selectByPage")
    public JSONResult selectByPage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer current,
                                   @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        IPage<Role> page = new Page<>(current, limit);
        roleService.page(page, null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());

        return JSONResult.ok(map);
    }


    @ApiOperation(value = "删除", notes = "根据id删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的id", required = true)
    })
    @DeleteMapping("/deleteById/{id}")
    public JSONResult deleteById(@PathVariable Long id) {
        boolean isSuccess = roleService.removeById(id);
        return isSuccess ? JSONResult.ok("删除成功") : JSONResult.error("删除失败");
    }

    @PostMapping("/add")
    public JSONResult add(@RequestBody Role role) {
        boolean isSuccess = roleService.save(role);
        return isSuccess ? JSONResult.ok("添加成功") : JSONResult.error("添加失败");
    }

    @GetMapping("/selectById/{id}")
    public JSONResult selectById(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return JSONResult.ok(role);
    }

    @PutMapping("/update")
    public JSONResult update(@RequestBody Role role) {
        boolean isSuccess = roleService.updateById(role);
        return isSuccess ? JSONResult.ok("修改成功") : JSONResult.error("修改失败");
    }

    @ApiOperation(value = "查找用户的权限", notes = "根据userId查找所有的权限，包括这个用户已经选择的权限")
    @GetMapping("/selectAssignedRole/{userId}")
    public JSONResult selectAssignedRole(@PathVariable Long userId) {
        Map<String, Object> map = roleService.selectAssignedRole(userId);
        return JSONResult.ok(map);
    }

    @PostMapping("/assignRole")
    public JSONResult assignRole(Long userId, Long[] roleIds) {
        roleService.assginRole(userId, roleIds);
        return JSONResult.ok("分配成功");
    }
}

